package com.work.keep.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.work.keep.R;
import com.work.keep.activity.HotDynamicActivitiy;
import com.work.keep.activity.OnRecyclerItemClickListener;
import com.work.keep.adapter.MyRecyclerAdapter;
import com.work.keep.adapter.OnColorChangable;
import com.work.keep.base.BaseFragment;
import com.work.keep.datas.DynamicDiscoverData;
import com.work.keep.datas.DynamicUserWallData;
import com.work.keep.datas.DynamicViewPagerData;
import com.work.keep.ui.PullToRefreshBase;
import com.work.keep.ui.PullToRefreshRecyclerView;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/12.
 */
public class DiscoverFragment extends BaseFragment {
    private Context context;
    private RecyclerView recyclerView;
    private MyRecyclerAdapter myRecyclerAdapter;
    private ScrollView scrollView;
    private OnColorChangable onColorChangable;
    private static final String BANNER_REQUEST = "https://api.gotokeep.com/v1.1/banners";
    private static final String AUTHORIZATION_VIEWPAGER = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTY"
            + "zZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRW"
            + "cwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTN"
            + "pYmYvMCIsImlhdCI6MTQ1Mjc1NjE4MywiZXhwIjoxNDU1MzQ4MTgzLCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.1yGIkgvZ0teRv5"
            + "KmKAyIt6F7xjHV6pMHJXbkVcyoQYk";
    private static final String AUTHORIZATION = "Authorization";
    private static final String X_X = "X_X";
    private static final String X_X_VIEWPAGER = "486bba2566a02d3ae31ed4445366a873b89cf910";
    private static final String HOST_VIEWPAGER = "api.gotokeep.com";
    private static final String HOST = "Host";
    private VolleySingleton volleySingleton;
    private GridLayoutManager gridLayoutManager;
    private PullToRefreshRecyclerView pullToRefreshRecyclerView;


    public void setOnColorChangable(OnColorChangable onColorChangable) {
        this.onColorChangable = onColorChangable;
    }

    public MyRecyclerAdapter getMyRecyclerAdapter() {
        return myRecyclerAdapter;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.fragment_dynamic, null);
        pullToRefreshRecyclerView = new PullToRefreshRecyclerView(context);
        return pullToRefreshRecyclerView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
        recyclerView = (RecyclerView) pullToRefreshRecyclerView.getRefreshableView();
    }


    @Override
    protected void init() {
        pullToRefreshRecyclerView.setPullRefreshEnabled(true);
        pullToRefreshRecyclerView.setPullLoadEnabled(true);
        pullToRefreshRecyclerView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {

            }
        });
//        RequestQueue queue = Volley.newRequestQueue(context);
        volleySingleton = VolleySingleton.getInstance();
        myRecyclerAdapter = new MyRecyclerAdapter(context);
        gridLayoutManager = new GridLayoutManager(context, 2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return gridLayoutManager.getSpanCount();
                }
                if (position == 1) {
                    return gridLayoutManager.getSpanCount();
                }
                return 1;
            }
        });
        recyclerView.setLayoutManager(gridLayoutManager);
//        recyclerView.setLayoutManager(new LinearLayoutManager(context));
//        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(myRecyclerAdapter);


        initViewPagerData();
        initUserWall();

    }

    private void initUserWall() {
        String urlUserWall = "https://api.gotokeep.com/v1.1/rankinglist/user/wall?limit=6";
        GsonRequest<DynamicUserWallData> userWallDataGsonRequest = new GsonRequest<DynamicUserWallData>(urlUserWall, DynamicUserWallData.class, new Response.Listener<DynamicUserWallData>() {
            @Override
            public void onResponse(DynamicUserWallData response) {
                Log.d("dis","onr");
                myRecyclerAdapter.addUserWallData(response.getData());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
             Log.d("dis","one");
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headerUserWall = new HashMap<>();
                headerUserWall.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MjgyOTA2MSwiZXhwIjoxNDU1NDIxMDYxLCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.NyY62RG_YPmZyn5Lfu0NXQs3KtaVAXhr5hZLVzlWtMM");
                headerUserWall.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                headerUserWall.put("Host", "api.gotokeep.com");
                return headerUserWall;
            }
        };
        volleySingleton._addRequest(userWallDataGsonRequest);
    }

    private void initRecyclerData() {
        String url = "https://api.gotokeep.com/v1.1/timeline/hot";
        GsonRequest<DynamicDiscoverData> request = new GsonRequest<DynamicDiscoverData>(url, DynamicDiscoverData.class, new Response.Listener<DynamicDiscoverData>() {
            @Override
            public void onResponse(final DynamicDiscoverData response) {
                myRecyclerAdapter.addRecyclerData(response.getData());
                recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
//                        Log.e("aa", "" + dy + "");
                        View view = recyclerView.getChildAt(0);
                        RecyclerView.ViewHolder holder = recyclerView.getChildViewHolder(view);
                        int position = recyclerView.getChildAdapterPosition(view);
                        if (holder.getLayoutPosition() > 3) {
                            int color = ((MyRecyclerAdapter.MyViewHolder) holder).getColor();
                            if (onColorChangable != null) {
                                onColorChangable.changeColor(color);
                            }
                        }

                    }
                });
                myRecyclerAdapter.setOnRecyclerItemClickListener(new OnRecyclerItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        HotDynamicActivitiy.goToHotDynamicActivity(context, response.getData());
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("aaa", "eee");
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                headers.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MjU4OTkwNiwiZXhwIjoxNDU1MTgxOTA2LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.4I6FpbJT1_1fe8Z51oHt6Mv5t-3zXGGYhI1rMe3U8gM");
                headers.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                headers.put("Host", "api.gotokeep.com");
                headers.put("X-KEEP-VERSION", "2.8.1");
                headers.put("X-KEEP-CHANNEL", "keep");
                return headers;
            }


        };
        volleySingleton._addRequest(request);
    }

    private void initViewPagerData() {
        GsonRequest<DynamicViewPagerData> viewPagerGsonRequest = new GsonRequest<DynamicViewPagerData>(BANNER_REQUEST, DynamicViewPagerData.class, new Response.Listener<DynamicViewPagerData>() {
            @Override
            public void onResponse(DynamicViewPagerData response) {
                myRecyclerAdapter.addPagerDatas(response.getData());
                initRecyclerData();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                headers.put(AUTHORIZATION, AUTHORIZATION_VIEWPAGER);
                headers.put(X_X, X_X_VIEWPAGER);
                headers.put(HOST, HOST_VIEWPAGER);
                return headers;
            }
        };
        volleySingleton._addRequest(viewPagerGsonRequest);

    }


}
